package org.zhaya.sdk.processors;

import cn.hutool.core.util.IdUtil;
import io.logz.sawmill.Doc;
import io.logz.sawmill.ProcessResult;
import io.logz.sawmill.Processor;
import io.logz.sawmill.annotations.ProcessorProvider;
import io.logz.sawmill.utilities.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

import static java.util.Objects.requireNonNull;

@ProcessorProvider(type = "addUUID", factory = AddUUIDProcessor.Factory.class)
public class AddUUIDProcessor implements Processor {
    private final String targetField;

    public AddUUIDProcessor(String targetField) {
        this.targetField = requireNonNull(targetField, "targetField cannot be null");
    }

    @Override
    public ProcessResult process(Doc doc) throws InterruptedException {
        doc.addField(targetField, IdUtil.fastUUID());
        return ProcessResult.success();
    }

    public static class Factory implements Processor.Factory {

        @Override
        public Processor create(Map<String, Object> config) {
            Configuration conf = JsonUtils.fromJsonMap(Configuration.class, config);
            return new AddUUIDProcessor(conf.getTargetField());
        }
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Configuration implements Processor.Configuration {
        private String targetField;
    }

}